package com.xiaobao.coalproperty.module.user.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.user.model.entity.AssetListEntity;
import com.xiaobao.coalproperty.other.adapter.UserPropertyAdapter;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.xiaobao.coalproperty.other.util.UserHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class UserPropertyActivity extends BaseActivity {

    @BindView(R.id.user_property_recycler)
    XRecyclerView mXRecyclerView;
    @BindView(R.id.user_money)
    TextView tvMoney;

    private UserPropertyAdapter mUserPropertyAdapter;
    private int page = 1;

    @Override
    protected int getContentView() {
        return R.layout.activity_user_property;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("我的资产");
        showRight(true);
        setRight("委托管理");
        setRightClickListener(rightOnClickListener);

        mUserPropertyAdapter = new UserPropertyAdapter(this);
        mUserPropertyAdapter.setOnItemClickListener(mOnItemClickListener);

        mXRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mXRecyclerView.setLoadingListener(mLoadingListener);
        mXRecyclerView.setAdapter(mUserPropertyAdapter);

        if (UserHelperUtils.getInstance().getCenter() != null) {
            tvMoney.setText(UserHelperUtils.getInstance().getCenter().getData().getCurrent_money());
        }
        update();
    }

    private XRecyclerView.LoadingListener mLoadingListener = new XRecyclerView.LoadingListener() {
        @Override
        public void onRefresh() {
            page = 1;
            update();
            mXRecyclerView.refreshComplete();
        }

        @Override
        public void onLoadMore() {
            update();
            mXRecyclerView.loadMoreComplete();
        }
    };

    private UserPropertyAdapter.onItemClickListener mOnItemClickListener = new UserPropertyAdapter.onItemClickListener() {
        @Override
        public void onItemClick(View view, int position) {
            startOrderActivity(mUserPropertyAdapter.getAdapterData().get(position).getId());
        }

        @Override
        public void onSaleClick(View view, int position) {
            startOrderActivity(mUserPropertyAdapter.getAdapterData().get(position).getId());
        }
    };

    private void startOrderActivity(String id) {
        Intent intent = new Intent(this, SaleOrderActivity.class);
        intent.setAction("id");
        intent.putExtra("id", id);
        startActivity(intent);
    }

    private View.OnClickListener rightOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(UserPropertyActivity.this, EntrustActivity.class));
        }
    };

    private void update() {
        OkHttpUtils.post().url(UrlConstant.URL_ASSETLIST)
                .params(HttpHelperUtils.getInstance().assetListParams(page + ""))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    AssetListEntity entity = new Gson().fromJson(response, AssetListEntity.class);
                    if (entity.getCode() == 0) {
                        if (entity.getData().getList().size() > 0) {
                            if (page == 1) {
                                mUserPropertyAdapter.setAdapterData(entity.getData().getList());
                            } else {
                                mUserPropertyAdapter.addAdapterData(entity.getData().getList());
                            }
                            page++;
                        }
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }
}
